
from core.celery import celery_app
from app.celery_task.models.celery_task import CeleryTask
from app.callback.handlers.callback_pod_heal_handler import CallbackPodHealHandler
from common.database import SessionLocal
from common.welab_k8s import init_k8s

from core.logger import logger



# @celery_app.task(bind=True)
# def k8s_pod_abnormal(self, data):
#     # 获取任务信息
#     tid = self.request.id
#     session = SessionLocal()
#     init_k8s()
#
#     # 开始同步阿里云账单
#     try:
#         # 检查数据库任务信息是否创建
#         result = CeleryTask.check_object_by_tid(session, tid)
#         if not result:
#             logger.error(f"tid:{tid} sync bill failed, database info not exist")
#             return False
#
#         # 更新状态
#         CeleryTask.update_task(session, tid, state="PROGRESS")
#
#         # 启动任务
#         prom_self_healing_handler = PromSelfHealingHandler()
#         result = prom_self_healing_handler.task_heal_pod_abnormal(session, tid, data)
#         if not result:
#             comment = f"{data['fingerprint']} => {data['container']} 删除pod: {data['pod_name']} 失败"
#             CeleryTask.update_task(session, tid, state="FAILURE", comment=comment)
#             logger.error(f"k8s_pod_abnormal {comment}")
#             return False
#     except Exception as e:
#         comment = f"{data['fingerprint']} => {data['container']} 删除pod: {data['pod_name']} 失败"
#         CeleryTask.update_task(session, tid, state="FAILURE", comment=comment)
#         logger.exception(f"k8s_pod_abnormal {comment} error => {e}")
#         return False
#
#     # 同步完成
#     comment = f"{data['fingerprint']} => {data['container']} 删除pod: {data['pod_name']} 成功"
#     CeleryTask.update_task(session, tid, state="SUCCESS", progress=100, comment=comment)
#     logger.info(f"k8s_pod_abnormal task:{tid} comment:{comment}")
#     return True


if __name__ == '__main__':
    pass
